package common.permission

object Permission {
    const val LOGIN = 0x0800  //bin:0 1000 0 0
    const val PUBLISH = 0x0400 //bin:0 0100 0 0
    const val BUY = 0x0200 //bin:0 0010 0 0
    const val COMMENT = 0x0100 //bin:0 0001 0 0

    private val defaultPermission = mutableListOf<Int>()

    init {
        defaultPermission.add(LOGIN)
        defaultPermission.add(PUBLISH)
        defaultPermission.add(BUY)
        defaultPermission.add(COMMENT)
    }

    fun initUserDefaultPermission(): Int{
        var permission = 0
        defaultPermission.forEach {
            permission = permission or it
        }
        return permission
    }

    fun hasPermission(state: Int, permission: Int): Boolean {
        return state and permission != 0
    }

    fun addPermission(state: Int, permission: Int): Int {
        return state or permission
    }

    fun removePermission(state: Int, permission: Int): Int {
        return state and permission.inv()
    }
}